package com.le.tester.book.concurrentjavafight.l_14;

/**
 * createTime：2024/5/16 15:16
 * description：使用wait和notifyAll来实现可以重新关闭的阀门
 */
public class ThreadGate {

    private boolean isOpen;

    private int generation;

    public synchronized void open() {
        ++generation;

        isOpen = true;

        notifyAll();
    }

    public synchronized void close() {
        isOpen = false;
    }

    //阻塞并直到：opened-since(generation on entry)

    public synchronized void await() throws InterruptedException {
        int arrivalGeneration = generation;
        while (!isOpen && arrivalGeneration == generation) {
            wait();

        }
    }
}
